package cn.chenhao.user.service.impl;

import cn.chenhao.basic.exception.BusinessException;
import cn.chenhao.basic.service.impl.BasicServiceImpl;
import cn.chenhao.basic.util.MD5Utils;
import cn.chenhao.basic.util.StrUtils;
import cn.chenhao.user.constant.UserConstant;
import cn.chenhao.user.domain.Logininfo;
import cn.chenhao.user.domain.User;
import cn.chenhao.user.dto.UserDto;
import cn.chenhao.user.mapper.LogininfoMapper;
import cn.chenhao.user.mapper.UserMapper;
import cn.chenhao.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl extends BasicServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper loginInfoMapper;

    /**
     * 修改用户信息的所有操作，都要级联修改loginInfo
     */

    @Override
    public void add(User user) {
        super.add(user);
    }

    @Override
    public void update(User user) {
        super.update(user);
    }

    @Override
    public void delete(Long id) {
        super.delete(id);
    }

    @Override
    @Transactional
    public void register(UserDto userDto) {
        // 一：校验
        // 1.空值校验
        if(StringUtils.isEmpty(userDto.getPhone())
                || StringUtils.isEmpty(userDto.getVerifyCode())
                || StringUtils.isEmpty(userDto.getPassword())
                || StringUtils.isEmpty(userDto.getConfirmPwd())){
            throw new BusinessException("请输入完整信息！");
        }
        // 2.两次密码是否一致
        if(!userDto.getPassword().equals(userDto.getConfirmPwd())){
            throw new BusinessException("两次密码不一致！");
        }
        // 3.手机不能被注册
        User userTmp = userMapper.loadByPhone(userDto.getPhone());
        if(userTmp != null){
            throw new BusinessException("用户已注册，请直接登录！");
        }
        // 4.验证是否过期
        Object codeTmp = redisTemplate.opsForValue().get(UserConstant.BUSINESS_REGISTER_PREFIX + userDto.getPhone());
        if(codeTmp == null){
            throw new BusinessException("验证码已过期，请重新获取！");
        }
        // 5.验证码是否相等   不要区分大小写
        String code = codeTmp.toString().split(":")[0];
        if(!userDto.getVerifyCode().equalsIgnoreCase(code)){
            throw new BusinessException("验证码输入错误！");
        }

        // 二：保存数据
        User user = userDto2User(userDto);
        Logininfo loginInfo = user2LoginInfo(user);
        // 1.保存loginInfo
        loginInfoMapper.save(loginInfo);//返回自增id
        // 2.保存user
        user.setLogininfo_id(loginInfo.getId());
        userMapper.save(user);
    }

    private Logininfo user2LoginInfo(User user) {
        Logininfo loginInfo = new Logininfo();
        // 属性同名原则拷贝
        BeanUtils.copyProperties(user, loginInfo);
        loginInfo.setType(1);
        return loginInfo;
    }


    private User userDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        // 盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 使用盐值加密之后的密码
        String md5Pwd = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        return user;
    }

}
